package com.dotstone.chipism.bean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.dotstone.chipism.util.DeviceManager;
import com.dotstone.chipism.util.SocketManager;

import android.util.Log;

/*************************************************************************
 * 公 司： 深圳市高金科技有限公司 作 者： 深圳市高金科技有限公司 文件名称：Wlan.java 内容摘要：智能网关模型 日 期： 2016-11-28
 ************************************************************************/
public class Device implements Serializable {
    private  String switchName;
    private  String switchName2;
    private  String switchName3;
    private   Map    switchMap;

	public String getSwitchName() {
		return switchName;
	}

	public void setSwitchName(String switchName) {
		this.switchName = switchName;
	}


	public Map getSwitchMap() {
		return switchMap;
	}

	public void setSwitchMap(Map switchMap) {
		this.switchMap = switchMap;
	}

	private String deviceID;
	private String deviceWifiSSID;// 超级精灵绑定的wifi ssid
	private String deviceAddress;// 设备mac地址
	private String deviceName;// 设备名称
	private String mainDeviceId;
	private int deviceType;// 设备类型（是我们自己定义的）
	private String deviceBrand;// 设备品牌 （遥控器使用这个）
	private int status;// 设备状态 0关 1开
	private boolean power;
	public boolean existed = false; // 搜索的时候用
	public String group;
	private boolean selected = false;
	private String closeCmd;
	private String openCmd;
	private String checkCmd = "";
	private byte[] opendata;
	private byte[] closedata;
	private byte[] checkdata;
	private int[] dataCmd;
	private byte[] deviceAdd;
	private byte[] cmdData;
	private byte[] data2;
	private int classification;
	private List<Key> Keys;
	private List<Device> childDevices = new ArrayList<Device>();
	private int role;// 1 自主添加 2被分享
	private int shareType; // 1家人分享 2 朋友分享
	private String userId;

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public List<Key> getKeys() {
		return Keys;
	}

	public void setKeys(List<Key> Keys) {
		Log.i("wmy", "获取按键" + Keys.size());
		this.Keys = Keys;
	}

	public String getGroup() {
		return group;
	}

	public void setGroup(String group) {
		this.group = group;
	}

	public int getRole() {
		return role;
	}

	public void setRole(int role) {
		this.role = role;
	}

	public int getShareType() {
		return shareType;
	}

	public void setShareType(int shareType) {
		this.shareType = shareType;
	}

	public String getDeviceWifiSsid() {
		return deviceWifiSSID;
	}

	public void setDeviceWifiSsid(String deviceWifiSSID) {
		this.deviceWifiSSID = deviceWifiSSID;
	}

	public String getDeviceAddress() {
		return deviceAddress;
	}

	public String getCheckCmd() {
		return checkCmd;
	}

	public void setCheckCmd(String checkCmd) {
		this.checkCmd = checkCmd;
	}

	public void setDeviceAddress(String deviceAddress) {
		// 获取到地址后
		this.deviceAddress = deviceAddress;
		// Log.i("wmy", deviceAddress + "63 " + this.deviceType);
		// Log.i("wmy", );
		if (deviceType == DeviceType.DEVICE_TYPE_SWITCH || deviceType == DeviceType.DEVICE_TYPE_SOCKET
				|| deviceType == DeviceType.DEVICE_TYPE_RGB_LIGHT || deviceType == DeviceType.DEVICE_TYPE_CURTAIN || deviceType == DeviceType.DEVICE_TYPE_CHAZUO
				|| deviceType == DeviceType.LINGHUO_SWITCH_1 || deviceType == DeviceType.LINGHUO_SWITCH_2 || deviceType == DeviceType.LINGHUO_SWITCH_3
				|| deviceType == DeviceType.DANHUO_SWITCH_1 || deviceType == DeviceType.DANHUO_SWITCH_2 || deviceType == DeviceType.DANHUO_SWITCH_3) {
			switch (deviceType) {

			case DeviceType.DEVICE_TYPE_SOCKET:
				opendata = new byte[] { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x10, (byte) 0xff, 0x00, 0x00 };
				closedata = new byte[] { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x10, 0x00, 0x00, 0x00 };
				checkdata = new byte[] { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x11, 0x00, 0x00, 0x00 };
				break;
			case DeviceType.DEVICE_TYPE_RGB_LIGHT:
				opendata = new byte[] { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x79, (byte) 0xff, 0x00, 0x00 };
				closedata = new byte[] { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x78, 0x00, 0x00, 0x00 };
				checkdata = new byte[] { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x87, 0x00, 0x00, 0x00 };
				break;
		    	case DeviceType.DEVICE_TYPE_LOCK:
				break;
				case DeviceType.DEVICE_TYPE_CURTAIN:
                    opendata = new byte[] { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x11, (byte) 0x02, 0x00, 0x00 };
                    closedata = new byte[] { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x11, 0x01, 0x00, 0x00 };
                    checkdata = new byte[] { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x15, 0x1, 0x00, 0x00 };
                    break;
				case DeviceType.DEVICE_TYPE_CHAZUO:
					opendata = new byte[] { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x10, (byte) 0xff, 0x00, 0x00 };
					closedata = new byte[] { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x10, 0x00, 0x00, 0x00 };
					checkdata = new byte[] { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x11, 0x00, 0x00, 0x00 };
					break;
				case DeviceType.DEVICE_TYPE_SWITCH:
					opendata = new byte[]{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x10, (byte) 0xff, 0x00, 0x00};
					closedata = new byte[]{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x10, (byte) 0x00, 0x00, 0x00};
					checkdata = new byte[]{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x20, (byte) 0x0, 0x00, 0x00};//查询开关状态
					break;
				case DeviceType.LINGHUO_SWITCH_1:
				case DeviceType.LINGHUO_SWITCH_2:
				case DeviceType.LINGHUO_SWITCH_3:
					opendata = new byte[]{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x10, (byte) 0xff, 0x00, 0x00};
					closedata = new byte[]{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x10, (byte) 0x00, 0x00, 0x00};
					checkdata = new byte[]{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x20, (byte) 0x0, 0x00, 0x00};//查询开关状态
					break;
				case DeviceType.DANHUO_SWITCH_1:
				case DeviceType.DANHUO_SWITCH_2:
				case DeviceType.DANHUO_SWITCH_3:
					opendata = new byte[]{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x10, (byte) 0xff, 0x00, 0x00};
					closedata = new byte[]{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x10, (byte) 0x00, 0x00, 0x00};
					checkdata = new byte[]{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x20, (byte) 0x0, 0x00, 0x00};//查询开关状态
					break;
			default:
				break;
			}
			data2 = getPowerData(opendata, deviceAddress);
			dataCmd = byte2HexStr(data2);
			openCmd = transCmd(dataCmd, deviceType);

			data2 = getPowerData(closedata, deviceAddress);
			dataCmd = byte2HexStr(data2);
			closeCmd = transCmd(dataCmd, deviceType);

			data2 = getPowerData(checkdata, deviceAddress);
			dataCmd = byte2HexStr(data2);
			checkCmd = transCheckCmd(deviceID, dataCmd, deviceType, mainDeviceId);
		}
	}

	public String getDeviceName() {
		return deviceName;
	}

	public void setDeviceName(String deviceName) {
		this.deviceName = deviceName;
	}

	public String getCloseCmd() {
		return closeCmd;
	}

	public void setCloseCmd(String closeCmd) {
		this.closeCmd = closeCmd;
	}

	public String getOpenCmd() {
		return openCmd;
	}

	public void setOpenCmd(String openCmd) {
		this.openCmd = openCmd;
	}

	public Device() {

	}

	public String getMainDeviceId() {
		return mainDeviceId;
	}

	public void setMainDeviceId(String mainDeviceId) {
		this.mainDeviceId = mainDeviceId;
	}

	public Device(String deviceID, String deviceAddress, String deviceName, int deviceType, int status) {
		super();
		this.deviceID = deviceID;
		this.deviceAddress = deviceAddress;
		this.deviceName = deviceName;
		this.deviceType = deviceType;
		this.status = status;
		// Log.i("wmy", deviceName + "144 " + this.deviceType);
		if (deviceType == DeviceType.DEVICE_TYPE_SWITCH || deviceType == DeviceType.DEVICE_TYPE_SOCKET
				|| deviceType == DeviceType.DEVICE_TYPE_RGB_LIGHT|| deviceType == DeviceType.DEVICE_TYPE_CURTAIN || deviceType == DeviceType.DEVICE_TYPE_CHAZUO
				|| deviceType == DeviceType.LINGHUO_SWITCH_1 || deviceType == DeviceType.LINGHUO_SWITCH_2 || deviceType == DeviceType.LINGHUO_SWITCH_3
				|| deviceType == DeviceType.DANHUO_SWITCH_1 || deviceType == DeviceType.DANHUO_SWITCH_2 || deviceType == DeviceType.DANHUO_SWITCH_3) {
			switch (deviceType) {
			case DeviceType.DEVICE_TYPE_SOCKET:
				opendata = new byte[] { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x10, (byte) 0xff, 0x00, 0x00 };
				closedata = new byte[] { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x10, 0x00, 0x00, 0x00 };
				checkdata = new byte[] { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x11, 0x00, 0x00, 0x00 };
				break;
			case DeviceType.DEVICE_TYPE_RGB_LIGHT:
				opendata = new byte[] { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x79, (byte) 0xff, 0x00, 0x00 };
				closedata = new byte[] { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x78, 0x00, 0x00, 0x00 };
				checkdata = new byte[] { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x87, 0x00, 0x00, 0x00 };
				break;
			case DeviceType.DEVICE_TYPE_LOCK:

				break;
                case DeviceType.DEVICE_TYPE_CURTAIN:
                    opendata = new byte[] { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x11, (byte) 0x1, 0x00, 0x00 };
                    closedata = new byte[] { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x11, 0x2, 0x00, 0x00 };
                    checkdata = new byte[] { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x15, 0x1, 0x00, 0x00 };
                    break;
				case DeviceType.DEVICE_TYPE_CHAZUO:
					opendata = new byte[] { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x10, (byte) 0xff, 0x00, 0x00 };
					closedata = new byte[] { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x10, 0x00, 0x00, 0x00 };
					checkdata = new byte[] { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x11, 0x00, 0x00, 0x00 };
					break;
				case DeviceType.DEVICE_TYPE_SWITCH:
					opendata = new byte[]{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x10, (byte) 0xff, 0x00, 0x00};
					closedata = new byte[]{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x10, (byte) 0x00, 0x00, 0x00};
					checkdata = new byte[]{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x20, (byte) 0x0, 0x00, 0x00};//查询开关状态
					break;
				case DeviceType.LINGHUO_SWITCH_1:
				case DeviceType.LINGHUO_SWITCH_2:
				case DeviceType.LINGHUO_SWITCH_3:
					opendata = new byte[]{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x10, (byte) 0xff, 0x00, 0x00};
					closedata = new byte[]{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x10, (byte) 0x00, 0x00, 0x00};
					checkdata = new byte[]{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x20, (byte) 0x0, 0x00, 0x00};//查询开关状态
					break;
				case DeviceType.DANHUO_SWITCH_1:
				case DeviceType.DANHUO_SWITCH_2:
				case DeviceType.DANHUO_SWITCH_3:
					opendata = new byte[]{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x10, (byte) 0xff, 0x00, 0x00};
					closedata = new byte[]{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x10, (byte) 0x00, 0x00, 0x00};
					checkdata = new byte[]{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x20, (byte) 0x0, 0x00, 0x00};//查询开关状态
					break;
				default:
				break;
			}
			if (deviceAddress != null) {
				data2 = getPowerData(opendata, deviceAddress);
				dataCmd = byte2HexStr(data2);
				openCmd = transCmd(dataCmd, deviceType);

				data2 = getPowerData(closedata, deviceAddress);
				dataCmd = byte2HexStr(data2);
				closeCmd = transCmd(dataCmd, deviceType);

				data2 = getPowerData(checkdata, deviceAddress);
				dataCmd = byte2HexStr(data2);
				checkCmd = transCheckCmd(deviceID, dataCmd, deviceType, mainDeviceId);
			}
		}
	}

	public int[] getDataOpenCmd(){
        data2 = getPowerData(opendata, deviceAddress);
		return byte2HexStr(data2);
	}

    public int[] getDataCloseCmd(){
        data2 = getPowerData(closedata, deviceAddress);
        return byte2HexStr(data2);
    }

	// public Device(String deviceID, String deviceWifiSSID, String
	// deviceAddress,
	// String deviceName, int deviceType, String deviceBrand, int status,
	// boolean deviceSelected, boolean existed) {
	// super();
	// this.deviceID = deviceID;
	// this.deviceWifiSSID = deviceWifiSSID;
	// this.deviceAddress = deviceAddress;
	// this.deviceName = deviceName;
	// this.deviceType = deviceType;
	// this.deviceBrand = deviceBrand;
	// this.status = status;
	// this.selected = selected;
	// this.existed = existed;
	// Log.i("wmy", deviceName+"237 "+deviceType);
	// if (deviceType == DeviceType.DEVICE_TYPE_SWITCH
	// || deviceType == DeviceType.DEVICE_TYPE_SOCKET
	// || deviceType == DeviceType.DEVICE_TYPE_RGB_LIGHT) {
	// switch (deviceType) {
	// case DeviceType.DEVICE_TYPE_SWITCH:
	// case DeviceType.DEVICE_TYPE_SOCKET:
	// opendata = new byte[] { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
	// (byte) 0x10, (byte) 0xff, 0x00, 0x00 };
	// closedata = new byte[] { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
	// (byte) 0x10, 0x00, 0x00, 0x00 };
	// break;
	// case DeviceType.DEVICE_TYPE_RGB_LIGHT:
	// opendata = new byte[] { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
	// (byte) 0x79, (byte) 0xff, 0x00, 0x00 };
	// closedata = new byte[] { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
	// (byte) 0x78, 0x00, 0x00, 0x00 };
	// break;
	// case DeviceType.DEVICE_TYPE_LOCK:
	//
	// break;
	//
	// default:
	// break;
	// }
	// data2 = getPowerData(opendata, deviceAddress);
	// dataCmd = byte2HexStr(data2);
	// openCmd = transCmd(dataCmd);
	// data2 = getPowerData(closedata, deviceAddress);
	// dataCmd = byte2HexStr(data2);
	// closeCmd = transCmd(dataCmd);
	// }
	// }

	public String getDeviceBrand() {
		return deviceBrand;
	}

	public void setDeviceBrand(String deviceBrand) {
		this.deviceBrand = deviceBrand;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public int getDeviceType() {
		return deviceType;
	}

	public void setDeviceType(int deviceType) {
		// Log.i("wmy", deviceAddress + "294 " + deviceType);
		this.deviceType = deviceType;
	}

	public String getDeviceID() {
		return deviceID;
	}

	public void setDeviceID(String deviceID) {
		this.deviceID = deviceID;
	}

	public boolean isSelected() {
		return selected;
	}

	public void setSelected(boolean selected) {
		this.selected = selected;
	}

	public byte[] getPowerData(byte[] commandData, String deviceAddress) {
		int CMDLENGTH = 10;
		deviceAdd = hexStringToBytes(deviceAddress);
		cmdData = new byte[CMDLENGTH];
		System.arraycopy(commandData, 0, cmdData, 0, CMDLENGTH);
		System.arraycopy(deviceAdd, 0, cmdData, 1, 5);
		return cmdData;
	}

	public static byte[] hexStringToBytes(String hexString) {
		if (hexString == null || hexString.equals("")) {
			return null;
		}
		hexString = hexString.toUpperCase();
		int length = hexString.length() / 2;
		char[] hexChars = hexString.toCharArray();
		byte[] d = new byte[length];
		for (int i = 0; i < length; i++) {
			int pos = i * 2;
			d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
		}
		return d;
	}

	private static byte charToByte(char c) {
		return (byte) "0123456789ABCDEF".indexOf(c);
	}

	public static int[] byte2HexStr(byte[] b) {
		int[] cmd = new int[b.length];
		String stmp = "";
		StringBuilder sb = new StringBuilder("");
		for (int n = 0; n < b.length; n++) {
			stmp = Integer.toHexString(b[n] & 0xFF);
			sb.append((stmp.length() == 1) ? stmp : stmp);
			sb.append(",");
			cmd[n] = Integer.parseInt(stmp, 16);
		}
		return cmd;
	}

	private static String transCheckCmd(String deviceID, int data[], int deviceType, String mainDeviceId) {
		String sendStr = "";
		JSONObject jo = new JSONObject();
		JSONObject jo1 = new JSONObject();
		try {
			jo.put("a", "20000");
			jo.put("b", Integer.parseInt(deviceID));
			jo.put("c", "20," + SocketManager.getInstance().user_id);
			jo.put("d", "10," + mainDeviceId);
			jo.put("e", 1);
			jo.put("f", System.currentTimeMillis());
			jo.put("h", "0.0.1");
			if (deviceType != DeviceType.DEVICE_TYPE_RGB_LIGHT) {
				jo1.put("cmd", 100);
			} else {
				jo1.put("cmd", 100);
			}
			if (data != null && data.length != 0) {
				JSONArray ja = new JSONArray();
				for (int i = 0; i < data.length; i++) { // 依次将数组元素添加进JSONArray对象中
					ja.put(data[i]);
				}
				jo1.put("data", ja);
			}
			jo.put("g", "" + jo1 + "");
			sendStr = jo.toString();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sendStr;
	}

	private static String transCmd(int data[], int deviceType) {
		String sendStr = "";
		JSONObject jo = new JSONObject();
		JSONObject jo1 = new JSONObject();
		try {
			jo.put("a", "20000");
			jo.put("b", 0);
			jo.put("c", "20," + SocketManager.getInstance().user_id);
			jo.put("d", "10," + DeviceManager.getInstance().getmWlanId());
			jo.put("e", 1);
			jo.put("f", System.currentTimeMillis());
			jo.put("h", "0.0.1");
			if (deviceType != DeviceType.DEVICE_TYPE_LOCK) {
				jo1.put("cmd", 101);
			} else {
				jo1.put("cmd", 103);
			}
			if (data != null && data.length != 0) {
				JSONArray ja = new JSONArray();
				for (int i = 0; i < data.length; i++) { // 依次将数组元素添加进JSONArray对象中
					ja.put(data[i]);
				}
				jo1.put("data", ja);
			}
			jo.put("g", "" + jo1 + "");
			sendStr = jo.toString();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sendStr;
	}

	public List<Device> getChildDevices() {
		return childDevices;
	}

	public void setChildDevices(List<Device> childDevices) {
		this.childDevices = childDevices;
	}

	public boolean isPower() {
		return power;
	}

	public void setPower(boolean power) {
		this.power = power;
	}

	public void checkStatus() {
		if (SocketManager.getInstance().sMap.get(mainDeviceId) != null) {
			// if
			// (SocketManager.getInstance().sMap.get(mainDeviceId).isLocalSuccess())
			// {
			SocketManager.getInstance().sMap.get(mainDeviceId).sendMqttDataDelay(checkCmd);
			// }else{
			// SocketManager.getInstance().sendRemoteData(deviceId, content);
			// }
		}
	}

}
